package com.rosense.server.controller;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.rosense.client.i.WRCpu;
import com.rosense.client.i.WRIo;
import com.rosense.client.i.WRMem;
import com.rosense.client.i.WRNet;
import com.rosense.client.rpc.RPCCpuBean;
import com.rosense.client.rpc.RPCIoBean;
import com.rosense.client.rpc.RPCMemBean;
import com.rosense.client.rpc.RPCNetBean;
import com.rosense.commons.LangUtils;
import com.rosense.commons.NumberUtils;
import com.rosense.server.config.HostBean;
import com.rosense.server.config.HostCaches;
import com.rosense.server.util.EchartsUtils;

/**
 * 
 * @author 李岩飞
 * @email eliyanfei@126.com	
 * 2017年12月11日 下午1:43:25
 *
 */
@Controller
@RequestMapping("/data")
public class DataController extends BaseController {
	@RequestMapping(value = "/host", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, HostBean> host() {
		return HostCaches.hostMap;
	}

	@RequestMapping(value = "/onehost", method = RequestMethod.GET)
	@ResponseBody
	public List<Map<String, String>> onehost(String host, String type, String time) {
		return HostCaches.wrMap.get(type).readAll(host, time);
	}
	@RequestMapping(value = "/onehostLine", method = RequestMethod.GET)
	@ResponseBody
	public List<String> onehostLine(String host, String type, String time) {
		List<String> chartList = new ArrayList<String>();
		Map<String, String> map = HostCaches.wrMap.get(type).readOnDay(host, time);
		String path = path = request.getRealPath("resource") + File.separator + "/echarts/demo/clines.txt";
		if(type.equals("cpu")){
			path = path = request.getRealPath("resource") + File.separator + "/echarts/demo/cline.txt";
		}
		Map<String,Object> map1 = new HashMap<String, Object>();
		map1.putAll(map);
		map1.put("id", "line_history");
		String line = EchartsUtils.buildEcharts(path, map1);
		chartList.add(line);
		return chartList;
	}
	@RequestMapping(value = "/used", method = RequestMethod.GET)
	@ResponseBody
	public Map<String,Map<String, String>> used(String type, String time,String limit) {
		Map<String,Map<String, String>> map = HostCaches.wrMap.get(type).readAlls(time,limit);
		return map;
	}

	/**
	 * 仪表盘
	 * @param host
	 * @return
	 */
	@RequestMapping(value = "/detailRate", method = RequestMethod.GET)
	@ResponseBody
	public List<String> detailRate(String host) {
		List<String> chartList = new ArrayList<String>();
		Map<String, Object> map = new HashMap<String, Object>();

		HostBean hostBean = HostCaches.hostMap.get(host);
		if (hostBean != null) {
			RPCCpuBean cpuBean = (RPCCpuBean) hostBean.rpcMap.get("cpu");
			if (cpuBean != null) {
				map.put("cpu", NumberUtils.formatNumber("0.0", cpuBean.cpusage * 100));
			}
			RPCIoBean ioBean = (RPCIoBean) hostBean.rpcMap.get("io");
			if (ioBean != null) {
				map.put("io", (NumberUtils.formatNumber("0.0", ioBean.used * 100, ioBean.total)));
			}
			RPCMemBean memBean = (RPCMemBean) hostBean.rpcMap.get("mem");
			if (memBean != null) {
				map.put("mem", (NumberUtils.formatNumber("0.0", (Math.max(memBean.used - memBean.cached, 0)) * 100, memBean.total)));
			}
		}

		chartList.add(LangUtils.toDouble(map.get("cup"), 0)+"%");
		chartList.add(LangUtils.toDouble(map.get("io"), 0)+"%");
		chartList.add(LangUtils.toDouble(map.get("mem"), 0)+"%");
		return chartList;
	}

	@RequestMapping(value = "/temp", method = RequestMethod.GET)
	@ResponseBody
	public Map<String, HostBean> temp() {
		String host = "192.168.56.101";
		String str = "";
		//cpu
		WRCpu rc = new WRCpu();
		List<RPCCpuBean> list = (List<RPCCpuBean>) rc.read(host);
		RPCCpuBean cpu = list.get(list.size() - 1);
		str += cpu.cpusage;
		HostCaches.hostMap.get(host).rpcMap.put(cpu.getName(), cpu);
		//mem
		WRMem mem = new WRMem();
		List<RPCMemBean> list1 = (List<RPCMemBean>) mem.read(host);
		RPCMemBean memb = list1.get(list1.size() - 1);
		str += "," + NumberUtils.formatNumber("0.00", memb.used, memb.total);
		HostCaches.hostMap.get(host).rpcMap.put(memb.getName(), memb);
		//io
		WRIo net = new WRIo();
		List<RPCIoBean> list2 = (List<RPCIoBean>) net.read(host);
		RPCIoBean iob = list2.get(list1.size() - 1);
		str += "," + NumberUtils.formatNumber("0.00", iob.used, iob.time);
		HostCaches.hostMap.get(host).rpcMap.put(iob.getName(), iob);
		System.out.println(host + ":" + str);
		return null;
	}

	@RequestMapping(value = "/disk", method = RequestMethod.GET)
	@ResponseBody
	public String disk(String host) {
		HostBean hostBean = HostCaches.hostMap.get(host);
		RPCIoBean ioBean = (RPCIoBean) hostBean.rpcMap.get("io");
		String str = "";
		if (ioBean == null) {
			return str;
		}
		Map<String, String> diskMap = ioBean.diskMap;
		for (String name : diskMap.keySet()) {
			String[] values = diskMap.get(name).split("_");
			double total = LangUtils.toDouble(values[0], 0) / 1024 / 1024;
			double used = LangUtils.toDouble(values[1], 0) / 1024 / 1024;
			str += "<tr><td  style='text-align: left;'>" + name + "</td><td>" + NumberUtils.formatNumber("0.000", total) + "</td>" + "<td>" + NumberUtils.formatNumber("0.00", used) + "</td><td>"
					+ NumberUtils.formatNumber("0.0%", used, total) + "</td></tr>";
		}
		return str;
	}

	@RequestMapping(value = "/detail1", method = RequestMethod.GET)
	@ResponseBody
	public List<String> detail(String host) {
		List<String> chartList = new ArrayList<String>();
		//折线图
		String path = request.getRealPath("resource") + File.separator + "/echarts/demo/cline.txt";
		Map<String, Object> map1 = new HashMap<String, Object>();
		//cpu
		WRCpu cpu = new WRCpu();
		List<RPCCpuBean> cpuList = (List<RPCCpuBean>) cpu.read(host);
		String xdata = "";
		String data = "";
		for (int i = 0; i < cpuList.size(); i++) {
			RPCCpuBean rc = cpuList.get(i);
			xdata += EchartsUtils.formatTime(rc.time);
			data += NumberUtils.formatNumber("0.00", rc.cpusage * 100);
			if (i < cpuList.size() - 1) {
				xdata += ",";
				data += ",";
			}
		}
		map1.put("id", "cpuline");
		map1.put("title", "CPU使用率");
		map1.put("xdata", xdata);
		map1.put("data", data);
		String line = EchartsUtils.buildEcharts(path, map1);
		chartList.add(line);
		//mem
		path = request.getRealPath("resource") + File.separator + "/echarts/demo/clines.txt";
		map1 = new HashMap<String, Object>();
		WRMem mem = new WRMem();
		List<RPCMemBean> memList = (List<RPCMemBean>) mem.read(host);
		xdata = "";
		data = "";
		String data2 = "";
		for (int i = 0; i < memList.size(); i++) {
			RPCMemBean rm = memList.get(i);
			xdata += EchartsUtils.formatTime(rm.time);
			data += NumberUtils.formatNumber("0.00", (Math.max(rm.used - rm.cached, 0)) * 100, rm.total);
			data2 += NumberUtils.formatNumber("0.00", rm.used * 100, rm.total);
			if (i < memList.size() - 1) {
				xdata += ",";
				data += ",";
				data2 += ",";
			}
		}
		map1.put("id", "memline");
		map1.put("title", "内存使用率");
		map1.put("name1", "不含缓存");
		map1.put("name2", "含缓存");
		map1.put("xdata", xdata);
		map1.put("data1", data);
		map1.put("data2", data2);
		line = EchartsUtils.buildEcharts(path, map1);
		chartList.add(line);
		return chartList;
	}

	@RequestMapping(value = "/detail2", method = RequestMethod.GET)
	@ResponseBody
	public List<String> detail2(String host) {
		List<String> chartList = new ArrayList<String>();
		//折线图
		String path = request.getRealPath("resource") + File.separator + "/echarts/demo/cline.txt";
		Map<String, Object> map1 = new HashMap<String, Object>();
		//			//io
		WRIo io = new WRIo();
		List<RPCIoBean> ioList = (List<RPCIoBean>) io.read(host);
		String xdata = "";
		String data = "";
		String data1 = "";
		for (int i = 0; i < ioList.size(); i++) {
			RPCIoBean rio = ioList.get(i);
			xdata += EchartsUtils.formatTime(rio.time);
			data += NumberUtils.formatNumber("0.00", rio.iousage);
			data1 += NumberUtils.formatNumber("0.00", rio.used * 100, rio.total);
			if (i < ioList.size() - 1) {
				xdata += ",";
				data += ",";
				data1 += ",";
			}
		}
		map1.put("id", "ioline");
		map1.put("title", "磁盘繁忙程度");
		map1.put("xdata", xdata);
		map1.put("data", data);
		String line = EchartsUtils.buildEcharts(path, map1);
		chartList.add(line);
		map1 = new HashMap<String, Object>();
		map1.put("id", "diskline");
		map1.put("xdata", xdata);
		map1.put("title", "磁盘使用率");
		map1.put("data", data1);
		line = EchartsUtils.buildEcharts(path, map1);
		chartList.add(line);
		return chartList;
	}

	@RequestMapping(value = "/detail3", method = RequestMethod.GET)
	@ResponseBody
	public List<String> detail3(String host) {
		List<String> chartList = new ArrayList<String>();
		//折线图
		String path = request.getRealPath("resource") + File.separator + "/echarts/demo/cline.txt";
		Map<String, Object> map1 = new HashMap<String, Object>();
		//				//net
		WRNet net = new WRNet();
		List<RPCNetBean> netList = (List<RPCNetBean>) net.read(host);
		String xdata = "";
		String data = "";
		String data1 = "";
		for (int i = 0; i < netList.size(); i++) {
			RPCNetBean rnet = netList.get(i);
			xdata += EchartsUtils.formatTime(rnet.time);
			data += NumberUtils.formatNumber("0.00", rnet.rx);
			data1 += NumberUtils.formatNumber("0.00", rnet.tx);
			if (i < netList.size() - 1) {
				xdata += ",";
				data += ",";
				data1 += ",";
			}
		}
		map1.put("id", "netinline");
		map1.put("title", "输入流量(M)");
		map1.put("xdata", xdata);
		map1.put("data", data);
		String line = EchartsUtils.buildEcharts(path, map1);
		chartList.add(line);
		map1 = new HashMap<String, Object>();
		map1.put("xdata", xdata);
		map1.put("id", "netoutline");
		map1.put("title", "输出流量(M)");
		map1.put("data", data1);
		line = EchartsUtils.buildEcharts(path, map1);
		chartList.add(line);
		return chartList;
	}
}
